package com.alliky.sign.ui.activity;


import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alliky.core.entity.UserEntity;
import com.alliky.core.router.RoutePath;
import com.alliky.core.base.BaseActivity;
import com.alliky.core.utils.StringUtils;
import com.alliky.core.utils.ToastUtils;
import com.alliky.core.widget.ClearEditText;
import com.alliky.core.widget.TitleBar;
import com.alliky.sign.R;
import com.alliky.sign.R2;
import com.alliky.sign.mvp.contract.LoginContract;
import com.alliky.sign.mvp.presenter.LoginPresenter;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = RoutePath.Sign.LOGIN_ACTIVITY)
public class LoginBaseActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {

    @BindView(R2.id.titleBar)
    TitleBar mTitleBar;

    @BindView(R2.id.et_username)
    ClearEditText et_username;
    @BindView(R2.id.et_password)
    ClearEditText et_password;

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void onInitView(@Nullable Bundle savedInstanceState) {
        mTitleBar.setLeftFinish(this);
    }

    @OnClick({R2.id.btn_login})
    public void onViewClick(View v) {
        if (v.getId() == R.id.btn_login) {
            if (StringUtils.isBlank(getUsername())) {
                ToastUtils.showShort("请输入账号");
                return;
            }
            if (StringUtils.isBlank(getPassword())) {
                ToastUtils.showShort("请输入密码");
                return;
            }

            mPresenter.login(getUsername(), getPassword());
        }
    }

    @Override
    public void loginResult(UserEntity userEntity) {
        if (userEntity != null) {
            ToastUtils.showShort("登录成功");
            this.finish();
        }
    }

    private String getUsername() {
        return et_username.getText().toString().trim();
    }

    private String getPassword() {
        return et_password.getText().toString().trim();
    }

}
